
<?php
/**
 * 
 * Enter description here ...
 * @author Zijian Wang
 */

class Friend_model extends CI_Model {

	function __construct()
	{
		// Call the Model constructor
		parent::__construct();
	}

  
	/**
	*
	* To create a new friendship request.
	* @param  $f_sender, $f_receiver
	* Note: $f_sender can send a friend request to $f_receiver. 
	* @param  $f_content can be NULL.
	* @param  $f_type 
	* Note: Certainly, $f_sender has to classify $f_receiver into a unique friend class($f_type,not NULL ),which   *       can be defined by $user_from.
	* Note: This is just a friend request, and $f_type's default value is 2(suspended) in the request. Only after  *       $f_receiver's confirmation ($f_response turns to be 1), this entry can be seen as a friendship record.
	* Note: send_time is created in the body of friendship_request().
	* @return ture/false
	*/
	function friendship_request($f_sender,$f_receiver,$f_content = NULL ,$f_type)
	{

		$send_time = gmmktime();

		if( ($f_sender == NUll) or ($f_receiver == NUll) or ($send_time == NULL) or ($f_type == NULL) )
		{
			// Comment Insertion is invalid!
			return false;
		}

        $sql = "INSERT INTO friend (f_sender,f_receiver,f_response,f_content,f_type,send_time,response_time) 
		VALUES ";
		$sql.= "('".$f_sender."','".$f_receiver."',2,'".$f_content."','".$f_type."',".$send_time.",NULL)"; 
		$result = $this->db->query($sql);
		return $result;
	}




   /**
	*
	* To confirm/decline a friendship request
	* @param  $f_sender, $f_receiver, $f_response
	* Note: If $f_receiver confirms a received friendship request, the $f_response will be set to 1,and this type  *           of friendship will be established. $f_receiver will get the permission from $f_sender.
	*       If $f_receiver declines this received friendship request,it becomes just a record. 
	* Note: response_time is created in the body of request_decision().
	* @return ture/false
	*/
	function request_decision($f_sender,$f_receiver,$f_response)
	{

		$response_time = gmmktime();

		if( ($f_sender == NULL) or ($f_receiver == NULL) or ($response_time == NULL) )
		{
			// Comment Insertion is invalid!
			return false;
		}

        $sql = "UPDATE friend SET f_response = '".$f_response."',response_time =".$response_time." WHERE f_sender = '".$f_sender."' ";
		$sql.= "AND f_receiver = '".$f_receiver."'";
		$result = $this->db->query($sql);
		return $result;
	}


   /**
	*
	* To terminate_friendship
	* @param  $f_sender, $f_receiver
	* Note: If $f_receiver decides to terminate_friendship, $f_response will be set to 0, 
	*       coresponding $f_receiver will lose the permission from $f_sender. The whole tuple becomes just a      *       record. 
	* @return ture/false
	*/
	function update_friendship($f_sender,$f_receiver,$f_type)
	{
		if( ($f_sender == NULL) or ($f_receiver == NULL) or !(($f_type == "train buddies" )or($f_type == "family") or($f_type == "college friends")) )
		{
			// Comment Insertion is invalid!
			return false;
		}

        $sql = "UPDATE friend SET f_type = '".$f_type."' WHERE f_sender = '".$f_sender."' ";
		$sql.= "AND f_receiver = '".$f_receiver."'";
		$result = $this->db->query($sql);
		return $result;
	}
	
   /**
	*
	* To eliminate requests record which are not anwsered or declined for some time. 
	* Note:can not delete the item because of database constraint.
	* @param  $f_response = 0 or 2; unit of $time is "day".
	* @return ture/false
	*/
	function eliminate_requests($f_response, $time)
	{

		$now = gmmktime();

		if( !(($f_response == 0) or ($f_response == 2)) )
		{
			return false;
		}

        $sql = "DELETE FROM friend WHERE f_response = ".$f_response." AND (".$now." - send_time > ";
		$sql.= $time."*24*3600 )";
		$result = $this->db->query($sql);
		return $result;
	}



   /**
	* To find $u_name's friends
	* @param $u_name 
	* @param $start,$end mean the scope of returned result.
	* @return an array.
	*/

	function friend_list($u_name, $start= 0, $end = 20)
	{
		if( $u_name == NULL )
		{
			return false;
		}

        $sql = "SELECT u.u_name AS u_name,u.u_username AS u_username, f.f_type AS f_type,f.f_sender AS f_sender
               FROM friend AS f,user AS u
               WHERE f.f_receiver = u.u_name AND f.f_sender ='".$u_name." ' AND f_response = 1
			   LIMIT ".$start.",".$end;
		$q = $this->db->query($sql);
		$result = $q->result_array($q);
		return $result;
	}


   /**
	* To find $u_name's friend
	* @param $u_name 
	* @param $start,$end mean the scope of returned result.
	* @return the query result.
	*/

	function show_friend_profile($u_name, $start= 0, $end = 20)
	{
		if( $u_name == NULL )
		{
			return false;
		}

        $sql = "SELECT u.u_name AS u_name,u.u_username AS u_username, f.f_type AS f_type
               FROM friend AS f,user AS u
               WHERE f.f_receiver = u.u_name AND f.f_sender ='".$u_name." ' AND f_response = 1
			   LIMIT ".$start.",".$end;
		$q = $this->db->query($sql);
		$result = $q->result_array($q);
		return $result;
	}


	/**
	* To list all of the unanswered requests of the user.
	* @param $user 
	* @param $start,$end mean the scope of returned result.
	* @return array of the query result.
	*/

	function unanswered_request($user, $start= 0, $end = 20)
	{
		if( $user == NULL )
		{
			return false;
		}

        $sql = "SELECT f.f_sender AS f_sender,u.u_username AS u_username, f.f_type AS f_type,f.f_content AS f_content
               FROM friend AS f,user AS u
               WHERE f.f_receiver = '".$user."' AND f.f_sender = u.u_name AND f_response = 2
			   LIMIT ".$start.",".$end;
		$q = $this->db->query($sql);
		$result = $q->result_array($q);
		return $result;
	}


	/**
	* To count unanswered requests of the user.
	* @param $user 
	* @return a sum number
	*/


	function unanswer_req_count($user)
	{
		if( $user == NUll )
		{

			return false;
		}

		$sql = "SELECT COUNT(f_receiver) AS sum ";
        $sql.= "FROM friend WHERE f_receiver = '".$user."' AND f_response =2";

		$q = $this->db->query($sql);
		$result = $q->result_array($q);

		$temp= intval($result[0]['sum']);
		return $temp;
	}
	function friend_count($user)
	{
		if( $user == NUll )
		{
			return false;
		}
	
		$sql = "SELECT COUNT(f_receiver) AS sum ";
		$sql.= "FROM friend WHERE f_sender = '".$user."' AND f_response =1";
	
		$q = $this->db->query($sql);
		$result = $q->result_array($q);
	
		$temp= intval($result[0]['sum']);
		return $temp;
	}
   /**
	* output all sub_classes in friends of $user 
	* @param $user 
	* @return sub_class
	*/


	function all_subclass($user)
	{
		if( $user == NUll )
		{
			return false;
		}

		$sql = "SELECT DISTINCT f_type ";
        $sql.= "FROM friend WHERE f_sender = '".$user."' AND f_response =1";

		$q = $this->db->query($sql);
		$result = $q->result_array($q);

		return $result;

	}


   /**
	* output the $user's friends in a subclass
	* @param $user, $f_type
	* @return sub_class
	*/
	function all_subclass_friend($user, $f_type)
	{
		if( $user == NUll or $f_type == NUll )
		{
			return false;
		}

		$sql = "SELECT f_receiver ";
        $sql.= "FROM friend WHERE f_sender = '".$user."' AND f_response =1 AND f_type ='".$f_type."'";

		$q = $this->db->query($sql);
		$result = $q->result_array($q);

		return $result;
	}



}

?>